//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var serviceId = GetQueryString("serviceId");
var serviceType = GetQueryString("serviceType");
var grade = GetQueryString("grade");
var paramType = GetQueryString("paramType");

var operateMethod = GetQueryString("operateMethod");
console.log("luowen operateMethod", operateMethod, isAddMode())

// here Test transfer data into
var jsonDataList = JSON.parse(decode_utf8(GetQueryString("jsonDataList")));
console.log("luowen dataList", jsonDataList)

var conditionType = GetQueryString("conditionType");

$(function(){
    //加载数据
    if(isAddMode()){

        $("#tb_fieldList").bootstrapTable({
            url : postPath + '/system/queryfield/getAllField',
            undefinedText : '-',
            striped : true, // 是否显示行间隔色
            queryParams : {serviceId:serviceId,serviceType:serviceType,grade:grade},
            uniqueId : "key", // 每一行的唯一标识
            columns:[{
                field : 'state',
                checkbox : true,
                align : 'center',
                valign : 'middle'
            },{
                title : '表名称',
                field : 'tableNameCn',
                align : 'center',
                valign : 'middle',
                visible: false
            },{
                title : '字段名称',
                field : 'fieldName',
                align : 'center',
                valign : 'middle'
            },{
                title : '字段中文名称',
                field : 'fieldNameCn',
                align : 'center',
                valign : 'middle'
            },{
                title : '表别名',
                field : 'tableAlterName',
                align : 'center',
                valign : 'middle',
                visible: false
            }, {
                title: '字段类型',
                field: 'fieldtype',
                align: 'center',
                valign: 'middle',
                sortable: false,
                visible: false
            }, {
                title: '参数Code',
                field: 'parametercode',
                align: 'center',
                valign: 'middle',
                sortable: false,
                visible: !isStaticCondition(),
                editable: {
                    type: 'text',
                    title: '参数Code',
                    validate: function (v) {
                    }
                }
            }, {
                title: '参数条件',
                field: 'parametercondition',
                align: 'center',
                valign: 'middle',
                sortable: false,
                editable: {
                    type: 'select',
                    source: [
                        {value: '=', text: '='},
                        {value: '!=', text: '!='},
                        {value: '>', text: '>'},
                        {value: '>=', text: '>='},
                        {value: '<', text: '<'},
                        {value: '<=', text: '<='},
                        {value: 'like', text: 'like'},
                    ],
                    'title': '参数条件'
                }
            }, {
                title: '参数值',
                field: 'parametervalue',
                align: 'center',
                valign: 'middle',
                width: '100px',
                sortable: false,
                visible : isStaticCondition(),
                editable: {
                    type: 'text',
                    title: '参数Code',
                    validate: function (v) {
                    }
                }
            },{
                title: '参数格式化',
                field: 'paramformat',
                align: 'center',
                valign: 'middle',
                sortable: false,
                visible: false,

            }, {
                title: '显示类型',
                field: 'showtype',
                align: 'left',
                valign: 'middle',
                sortable: false,
                visible: false,
                formatter: function (value, row, index) {
                    var result = "";
                    if (value == 0) {
                        result = "文本框";
                    } else if (value == 1) {
                        result = "下拉框";
                    } else if (value == 2) {
                        result = "单选按钮";
                    } else if (value == 3) {
                        result = "复选框";
                    } else if (value == 4) {
                        result = "日期框";
                    }
                    return result;
                }
            }, {
                title: '显示顺序',
                field: 'queryorder',
                align: 'left',
                valign: 'middle',
                width: '100px',
                sortable: false,
                visible: false
            }]
        });
    }else {

        $("#tb_fieldList").bootstrapTable({
            data: jsonDataList,
            undefinedText : '-',
            striped : true, // 是否显示行间隔色
            queryParams : {serviceId:serviceId,serviceType:serviceType,grade:grade},
            uniqueId : "key", // 每一行的唯一标识
            columns:[{
                field : 'state',
                checkbox : true,
                align : 'center',
                valign : 'middle'
            },{
                title : '表名称',
                field : 'tableNameCn',
                align : 'center',
                valign : 'middle',
                visible: false
            },{
                title: '字段名称',
                field: 'fieldname',
                align: 'center',
                valign: 'middle',
                sortable: false
            }, {
                title: '字段中文名称',
                field: 'fieldnameCn',
                align: 'left',
                valign: 'middle',
                sortable: false
            },{
                title : '表别名',
                field : 'tableAlterName',
                align : 'center',
                valign : 'middle',
                visible: false
            }, {
                title: '字段类型',
                field: 'fieldtype',
                align: 'center',
                valign: 'middle',
                sortable: false,
                visible: false
            }, {
                title: '参数Code',
                field: 'parametercode',
                align: 'center',
                valign: 'middle',
                sortable: false,
                visible: !isStaticCondition(),
                editable: {
                    type: 'text',
                    title: '参数Code',
                    validate: function (v) {
                    }
                }
            }, {
                title: '参数条件',
                field: 'parametercondition',
                align: 'center',
                valign: 'middle',
                sortable: false,
                editable: {
                    type: 'select',
                    source: [
                        {value: '=', text: '='},
                        {value: '!=', text: '!='},
                        {value: '>', text: '>'},
                        {value: '>=', text: '>='},
                        {value: '<', text: '<'},
                        {value: '<=', text: '<='},
                        {value: 'like', text: 'like'},
                    ],
                    'title': '参数条件'
                }
            }, {
                title: '参数值',
                field: 'parametervalue',
                align: 'center',
                valign: 'middle',
                width: '100px',
                sortable: false,
                visible : isStaticCondition(),
                editable: {
                    type: 'text',
                    title: '参数Code',
                    validate: function (v) {
                    }
                }


            },{
                title: '参数格式化',
                field: 'paramformat',
                align: 'center',
                valign: 'middle',
                sortable: false,
                visible: false,

            }, {
                title: '显示类型',
                field: 'showtype',
                align: 'left',
                valign: 'middle',
                sortable: false,
                visible: false,
                formatter: function (value, row, index) {
                    var result = "";
                    if (value == 0) {
                        result = "文本框";
                    } else if (value == 1) {
                        result = "下拉框";
                    } else if (value == 2) {
                        result = "单选按钮";
                    } else if (value == 3) {
                        result = "复选框";
                    } else if (value == 4) {
                        result = "日期框";
                    }
                    return result;
                }
            }, {
                title: '显示顺序',
                field: 'queryorder',
                align: 'left',
                valign: 'middle',
                width: '100px',
                sortable: false,
                visible: false
            }]
        });


    }
    // //新增查询字段
    // $("#btn_fieldAdd").click(function(){
    //     var row = $('#tb_fieldList').bootstrapTable('getSelections');
    //     if (row == null || row == "") {
    //         bootbox.alert("请选择一行!");
    //         return;
    //     }
    //     console.log(row);
    //     var strFields="";
    //     $.each(row,function(i,e){
    //         strFields+=e.fieldguid+"@";
    //     });
    //     strFields = strFields.substring(0,strFields.length-1);
    //     $.ajax({
    //         url:  postPath + '/system/queryfield/addQueryField',
    //         type: 'POST',
    //         data: { strFields: strFields,serviceId:serviceId,serviceType:serviceType,grade:grade,paramType:paramType },
    //         success: function (data) {
    //             // bootbox.alert("新增成功!");
    //             showMsg('新增成功!', 'success');
    //             $('#tb_fieldList').bootstrapTable('refresh');
    //             // parent.closeModul();
    //         },
    //         error:function() {
    //             // bootbox.alert("新增失败,请重试!");
    //             showMsg('新增失败,请刷新后重试!!', 'error');
    //         }
    //     });
    // });

    $("#btn_fieldUpdate").click(function () {
        // here get all field
        var rowList = $('#tb_fieldList').bootstrapTable('getData');
        console.log("luowen update rowList", rowList)
        $.ajax({
            type: 'POST',
            url: postPath + '/system/queryfield/batchUpdateQueryfieldList',
            async: false,
            data: JSON.stringify(rowList),
            dataType: 'json',
            contentType: "application/json",
            success: function (data) {
                $("#tb_queryfieldList").bootstrapTable('refresh');
                showMsg('修改成功!', 'success');

                //getQuerysql();
            }
        });
    });

    //新增查询字段
    $("#btn_fieldAdd").click(function(){
        var row = $('#tb_fieldList').bootstrapTable('getSelections');
        if (row == null || row == "") {
            bootbox.alert("请选择一行!");
            return;
        }
        console.log(row);
        var baseQueryFieldList = [];
        $.each(row,function(i,e){
            var baseQueryField = {
                guid: e.guid,
                fieldaddonsguid: e.fieldguid,
                serviceid: serviceId,
                servicetype: serviceType,
                fieldname: e.fieldName,
                fieldnameCn: e.fieldNameCn,
                fieldtype: e.fieldtype,
                parametercode: e.parametercode,
                parametercondition: e.parametercondition,
                parametervalue: e.parametervalue,
                paramformat: e.paramformat,
                showtype: e.showtype,
                queryorder: e.queryorder,
                grade: grade,
                showtype: 0,
                remark: ""
            }
            baseQueryFieldList.push(baseQueryField);
        });

        console.log("luowen baseQueryFieldList", baseQueryFieldList);
        $.ajax({
//            url:  postPath + '/system/queryfield/addQueryField?paramType='+paramType,
            url:  postPath + '/system/queryfield/batchAddQueryFieldBatch?paramType='+paramType,
            type: 'POST',
            contentType:"application/json",
            data: JSON.stringify(baseQueryFieldList),
            success: function (data) {
                // bootbox.alert("新增成功!");
                showMsg('新增成功!', 'success');
                $('#tb_fieldList').bootstrapTable('refresh');
                // parent.closeModul();
            },
            error:function() {
                // bootbox.alert("新增失败,请重试!");
                showMsg('新增失败,请刷新后重试!!', 'error');
            }
        });
    });

})

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}


function showMsg(text, icon) {
    $.toast().reset('all');
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600'
    });
}



// here is handle button
function isAddMode(){
    var isAddMode = operateMethod == 'add';
    var addButton = document.getElementById("btn_fieldAdd")
    var updateButton = document.getElementById("btn_fieldUpdate")

    if(isAddMode){
        // add show update hidden
        addButton.style.display = "block";
        updateButton.style.display = "none";

    }else{
        // update show add hidden
        addButton.style.display = "none";
        updateButton.style.display = "block";
    }
    return  isAddMode;
}

function isStaticCondition(){
    return conditionType == 'staticCondition';
}

function decode_utf8(s) {
  return decodeURIComponent(escape(s));
}
